import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:sports_new/server/api/user_api.dart';
import 'package:sports_new/utils/routes.dart';
import 'package:sports_new/utils/toast_utils.dart';

import '../../../entity/live/live_detail_entity.dart';
import '../../../resources/colours.dart';
import '../../../utils/utils.dart';
import '../../../utils/widget_utils.dart';
import '../../../widgets/circular.dart';
import '../../../widgets/base/qkd_image.dart';
import '../../../widgets/base/qkd_text.dart';
import 'live_message_list.dart';

class LiveExpertInfo extends StatefulWidget {
  const LiveExpertInfo({
    super.key,
    this.liveDetail,
    this.onFocusChanged,
    required this.scene,
  });

  const LiveExpertInfo.push({
    super.key,
    this.liveDetail,
  }):scene = MessageScene.push,onFocusChanged = null;

  const LiveExpertInfo.normal({
    super.key,
    this.liveDetail,
    this.onFocusChanged
  }):scene = MessageScene.normal;

  final LiveDetailEntity? liveDetail;
  final MessageScene scene;
  final ValueChanged<bool>? onFocusChanged;

  @override
  State<LiveExpertInfo> createState() => _LiveExpertInfoState();
}

class _LiveExpertInfoState extends State<LiveExpertInfo> {
  @override
  Widget build(BuildContext context) {
    return Circular(
      height: 36,
      margin: const EdgeInsets.only(left: 8),
      padding: const EdgeInsets.all(2),
      borderRadius: const BorderRadius.horizontal(left: Radius.circular(36)),
      gradient: LinearGradient(
        colors: [
          Colours.black.withOpacity(0.3),
          Colours.black.withOpacity(0)
        ],
        stops: const [0.2,0.85]
      ),
      child: GestureDetector(
        onTap: (){
          if(widget.scene == MessageScene.normal){
            Utils.onEvent('spzb',params: {'spzb':0});
            Get.toNamed(
              Routes.userPage,
              arguments: widget.liveDetail?.expertId,
              parameters: {"type":"2"}
            );
          }
        },
        child: Row(
          children: [
            QkdImage(
              imageUrl: widget.liveDetail?.expertLogo??"",
              width: 32,height: 32,
              shape: BoxShape.circle,
              fit: BoxFit.cover,
              shimmer: false,
            ),
            Container(width: 8),
            Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Flexible(
                  child: Container(
                    constraints: const BoxConstraints(maxWidth: 90),
                    child: QkdText(widget.liveDetail?.expertName,
                      fontSize: 12,color: Colours.white,
                      fontWeight: FontWeight.w500,
                      maxLines: 1,
                    ),
                  ),
                ),
                Opacity(
                  opacity: 0.5,
                  child: Row(
                    children: [
                      Image.asset(
                        Utils.getImgPath("live_hot.png"),
                        width: 12,height: 12,
                        color: Colours.white,
                      ),
                      QkdText("${widget.liveDetail?.hot}",
                        fontSize: 12,color: Colours.white,
                      ),
                    ],
                  ),
                )
              ],
            ),
            if(widget.scene == MessageScene.normal)
            GestureDetector(
              onTap: () async{
                Utils.onEvent('spzb',params: {'spzb':1});
                if(widget.liveDetail?.isFocus == 0) {
                  final result = await UserApi.focus(
                    expertId: widget.liveDetail?.expertId
                  );
                  if (result == 200) {
                    ToastUtils.show('关注成功');
                    widget.onFocusChanged?.call(true);
                    return;
                  }
                }else{
                  final r = await WidgetUtils.alertQuery('确认不再关注？');
                  if (r == true) {
                    final result = await UserApi.unFocus(
                      expertId: widget.liveDetail?.expertId
                    );
                    if (result == 200) {
                      ToastUtils.show('取消关注成功');
                      widget.onFocusChanged?.call(false);
                      return;
                    }
                  }
                }
              },
              child: Container(
                width: 40,height: 24,
                alignment: Alignment.center,
                decoration: BoxDecoration(
                  color: widget.liveDetail?.isFocus == 1?Colours.greyDD:null,
                  borderRadius: BorderRadius.circular(18),
                  gradient: widget.liveDetail?.isFocus == 1?null:const LinearGradient(
                    colors: [Color(0xFFF54D4F),Color(0xFFF53C6A)],
                  )
                ),
                child: QkdText(
                  widget.liveDetail?.isFocus == 1?"已关注":"关注",
                  fontSize: 10,
                  color: widget.liveDetail?.isFocus == 1?
                  Colours.grey99:Colours.white,
                ),
              ),
            ).paddingOnly(left: 8)
          ],
        ),
      ),
    );
  }
}
